package nckd.wnqjt.tmc.tda.plugin.form;

import com.alibaba.fastjson.JSON;
import kd.bos.algo.DataSet;
import kd.bos.algo.Row;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.entity.qing.*;
import kd.bos.form.IPageCache;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhujintao
 * @date 2024/11/05
 * @desc 路径：【资金云】-【资金调度】-【下拨处理】
 * * 取全部单据状态是已付款的下拨单笔数
 */
public class FcaTransdownbillQingDataProvider extends AbstractFormPlugin implements IQingDataProvider {


    private static Log log = LogFactory.getLog(FcaTransdownbillQingDataProvider.class);

    /**
     * 获取表头信息定义
     */
    @Override
    public QingMeta getMeta(String pageId) {
        String entityKey = "fca_transdownbill";
        QingMeta meta = new QingMeta();
        EntryEntity entity = new EntryEntity();
        entity.setKey(entityKey); //实体名(唯一标识)	是
        entity.setName(new LocaleString("下拨处理")); //实体别名	是
        entity.setPkFieldName("id");//主键名称	是
        meta.addEntryEntity(entity);

        Field column = new Field();
        column.setEntity(entityKey);//字段所属实体key	否
        column.setKey("id");// 字段名(唯一标识)	是
        column.setFieldType(QingFieldType.Int.toNumber()); //数据类型	是
        column.setName(new LocaleString("id")); //字段别名	是
        meta.addColumn(column);
        return meta;
    }

    /**
     * 获取轻分析数据
     *
     * @param startRow 起始行
     * @param limit    每页的数据量
     * @return 轻分析数据
     */
    @Override
    public QingData getData(String pageId, int startRow, int limit) {
        //拿过滤条件，进行取数
        String qFilter = getPageCache().get("qFilter");
        if (qFilter != null) {
            //通过此方法拿到过滤参数
            Map<String, Object> filterParam = (Map) JSON.parseObject(qFilter, Map.class);
        }
        QFilter acctstatusfilter = new QFilter("billstatus", QCP.equals, "D");
        DataSet transdownbillDataSet = QueryServiceHelper.queryDataSet(this.getClass().getName(), "fca_transdownbill", "id", acctstatusfilter.toArray(), null);
        DataSet count = transdownbillDataSet.groupBy().count("id").finish();
        QingData data = null;
        if (startRow < 200) {
            data = new QingData();
            Map<String, Integer> dataindex = new HashMap<String, Integer>();
            dataindex.put("id", 0);

            data.setDataindex(dataindex);

            List<Object[]> rows = new ArrayList<Object[]>();
            while (count.hasNext()) {
                Row row = count.next();
                Object[] obj = new Object[1];
                obj[0] = row.getLong("id");
                rows.add(obj);
            }
            data.setRows(rows);
        }
        return data;
    }


    @Override
    public void setFilterParameter(String filterParameter) {
        IPageCache pageCache = getPageCache();
        pageCache.put("qFilter", filterParameter);

    }

    @Override
    public String getFilterParameter() {
        return getPageCache().get("qFilter");
    }
}